# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: reporter\reports\executive_summary.py
from reporter.reports.basic_template import NamedTempFileWithNormalizedWrite
from reporter.reports.basic_template import ReportTemplate
from io import StringIO
import jinja2


class ExecutiveSummaryReport(ReportTemplate):
    REPORT_NAME = "Executive Summary Report"

    def parts(self):
        template_loader = jinja2.FileSystemLoader(searchpath="data/templates/reports/")
        template_env = jinja2.Environment(loader=template_loader)
        template_vars = {}
        template = template_env.get_template("executive_summary_template.html")
        common_parts_template_vars = {}
        common_parts_template = template_env.get_template("common_parts_template.html")
        data = StringIO()
        data.write(common_parts_template.render({"template_part": "header"}))
        common_parts_template_vars["template_part"] = "body_start"
        common_parts_template_vars["report_name"] = self.REPORT_NAME
        common_parts_template_vars["date_generated"] = self.get_formatted_nowdate()
        data.write(common_parts_template.render(common_parts_template_vars))
        data.seek(0)
        yield data
        data = StringIO()
        with NamedTempFileWithNormalizedWrite() as (file_items):
            for source in self.sources:
                alerts_count = {
                    self.SEVERITY_MAX: 0,
                    self.SEVERITY_MAX - 1: 0,
                    self.SEVERITY_MAX - 2: 0,
                    self.SEVERITY_MAX - 3: 0,
                }
                file_items.seek(0)
                file_items.truncate()
                file_items.write(
                    template.render({"template_part": "executive_summary_title"})
                )
                file_items.write(template.render({"template_part": "table_opening"}))
                vulnerabilities = {
                    self.SEVERITY_MAX: [],
                    self.SEVERITY_MAX - 1: [],
                    self.SEVERITY_MAX - 2: [],
                    self.SEVERITY_MAX - 3: [],
                }
                for vulnerability_type, cnt in source.vulnerability_types():
                    alerts_count[vulnerability_type["severity"]] += cnt
                    vulnerability = [vulnerability_type["name"], cnt]
                    vulnerabilities[vulnerability_type["severity"]].append(
                        vulnerability
                    )

                group_name_index = 0
                count_index = 1
                for severity in range(self.SEVERITY_MAX, 0, -1):
                    sorted_group = sorted(
                        vulnerabilities[severity],
                        key=lambda a: a[count_index],
                        reverse=True,
                    )
                    for vuln in sorted_group:
                        template_vars["alert_group"] = vuln[group_name_index]
                        template_vars["alert_count"] = vuln[count_index]
                        template_vars["severity_string"] = self.get_severity_string(
                            severity
                        )
                        template_vars["template_part"] = "item"
                        file_items.write(template.render(template_vars))

                file_items.write(template.render({"template_part": "table_closing"}))
                common_parts_template_vars["template_part"] = "display_scan_info"
                common_parts_template_vars[
                    "threat_level"
                ] = self.calculate_threat_level(alerts_count)
                common_parts_template_vars[
                    "scan_info"
                ] = self.get_well_formed_scan_info(source)
                common_parts_template_vars["alerts_count"] = alerts_count
                data.write(common_parts_template.render(common_parts_template_vars))
                data.seek(0)
                yield data
                data = StringIO()
                file_items.seek(0)
                yield file_items

        data.write(common_parts_template.render({"template_part": "body_end"}))
        data.seek(0)
        yield data
